﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Common_ERcodeLendBooks : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userName"]==null)
        {
             Response.Write("<script language=javascript>alert('您尚未登陆，不能借书，请完成登陆后再借阅');window.location = 'BMindex.aspx';</script>");
        } 
        else
        {
            if (Request.QueryString["BookID"]==null)
            {
                Response.Write("<script language=javascript>alert('该书不存在，请刷新二维码');window.location = 'BMindex.aspx';</script>");
            } 
            else
            {
                string bookT = Request.QueryString["BookID"].ToString();
                SQL sql = new SQL();
                string psw = sql.getDataFormTable("password", "reader_info", "readerID", Session["userName"].ToString(), "", "", "", "", 1);
                int booknum = Convert.ToInt32(sql.getDataFormTable("CURRENTNUM", "book_info", "bookID", bookT, "", "", "", "", 1));
                int BeborrowTimes = Convert.ToInt32(sql.getDataFormTable("BeBorrowTimes", "book_info", "bookID", bookT, "", "", "", "", 1));
                //执行步骤：
                //1、检查用户是否登陆
                //2、检查密码是否正确
                //3、检查图书是否在库
                //4、检查图书剩余数量
                //5、借阅单号写入借阅单表
                //6\检查是否超过自己读者类型的最大限制本数量
                //6、图书信息表修改目前图书数量，图书状态，图书被借次数

                    string status = sql.getDataFormTable("statusID", "book_info", "bookID", bookT, "", "", "", "", 1);
                    if (status.Equals("1"))
                    {
                        int kejieyuetushu = 0;
                        string typeid = sql.getDataFormTable("ReaderType", "Reader_info", "readerid", Session["userName"].ToString(), "", "", "", "", 1);
                        kejieyuetushu = Convert.ToInt32(sql.EXQSQL("select maxlend from reader_type where readertype=" + typeid + ""));
                        if (Convert.ToInt32(sql.EXQSQL("select count(*) from Borrow_books_manage where readerID='" + Session["userName"].ToString() + "' and returndate is null ")) <= kejieyuetushu)
                        {
                            if (Convert.ToInt32(sql.getDataFormTable("CURRENTNUM", "book_info", "bookID", bookT, "", "", "", "", 1)) > 0)
                            {
                                string shouldReturnday = DateTime.Now.AddDays(Convert.ToInt32(sql.EXQSQL("select Borrowdays from System_state"))).ToString();
                                sql.updateBOOKinfo("insert into Borrow_books_manage(BookID,ReaderID,DDNUM,LendBookDate,ShouldReturnDate,XUJIETIMES) values('" + bookT + "','" + Session["userName"].ToString() + "','" + bookT + Session["userName"].ToString() + string.Format("{0:yyyymmddhhmmss}", DateTime.Now) + "','" + DateTime.Now.ToString() + "','" + shouldReturnday + "','0')");//写入借阅单表

                                sql.EXQSQL("update Book_info set CurrentNum=" + (booknum - 1) + ",statusid=2,BeBorrowTimes=" + (BeborrowTimes + 1) + " where bookid=" + bookT + "");
                                Response.Write("<script language=javascript>alert('完成借阅');</script>");
                            }
                            else
                            {
                                Response.Write("<script language=javascript>alert('图书已经全部借出');</script>");
                            }
                        }
                        else
                        {
                            Response.Write("<script language=javascript>alert('您已经超过最大可借数量');</script>");

                        }

                    }
                    else
                    {
                        Response.Write("<script language=javascript>alert('图书不在库');</script>");
                    }
                

            }
        }
        
    }
}